﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RedfieldWeather
{
	public partial class _Default : System.Web.UI.Page
	{
		private RFW.Weather.Alerts _alerts = new RFW.Weather.Alerts();
		private RFW.Weather.Forecast.Forecast _forecast = new RFW.Weather.Forecast.Forecast();

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
				LoadData();
		}

		private void LoadData()
		{
			_alerts.DownloadComplete += new EventHandler(_alerts_DownloadComplete);
			_alerts.Get();

			_forecast.DownloadComplete += new EventHandler(_forecast_DownloadComplete);
			_forecast.Get();
		}

		void _currentConditions_DownloadComplete(object sender, EventArgs e)
		{
			System.Diagnostics.Debug.WriteLine("Current Conditions Downloaded");
		}

		void _forecast_DownloadComplete(object sender, EventArgs e)
		{
			rptForecast.DataSource = _forecast.Today;
			rptForecast.DataBind();

			lblSunset.Text = _forecast.Moon.Sunset;
			lblSunrise.Text = _forecast.Moon.Sunrise;
			lblMoonAge.Text = _forecast.Moon.Age;
			lblMoonPct.Text = _forecast.Moon.PercentIlluminated;
		}

		void _alerts_DownloadComplete(object sender, EventArgs e)
		{
			if (_alerts.Items != null && _alerts.Items.Count > 0)
			{
				divAlerts.Visible = true;

				rptAlerts.DataSource = _alerts.Items;
				rptAlerts.DataBind();
			}
		}

		protected void Timer1_Tick(object sender, EventArgs e)
		{
			CurrentForecast.UpdateForecast();
		}
	}
}
